package zero.boot.freemarkerdemo.core.log.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Component
@Aspect
public class CallLogAspect {
    private ThreadLocal<Object> map = null;


    /**
     * 前置通知
     *
     * @param joinPoint 切点
     */
    @Before("@annotation(zero.boot.freemarkerdemo.core.log.annotation.CallLog)")
    public void beforeMethod(JoinPoint joinPoint) {
       /* String methodName = joinPoint.getSignature().getName();
        List<Object> args = Arrays.asList(joinPoint.getArgs());
        System.out.println("beforeMethod " + methodName + " and " + args);
        map = new ThreadLocal<>();*/

        try {
            HttpServletRequest req = null;
            HttpServletResponse resp = null;

            RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
            if (requestAttributes != null) {
                ServletRequestAttributes attr = (ServletRequestAttributes) requestAttributes;
                req = attr.getRequest();
                resp = attr.getResponse();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    /**
     * 返回通知
     *
     * @param joinPoint 切点
     * @param result    方法返回结果
     */

    @AfterReturning(value = "@annotation(zero.boot.freemarkerdemo.core.log.annotation.CallLog)", argNames = "joinPoint,result", returning = "result")
    public void afterReturnMethod(JoinPoint joinPoint, Object result) {

        System.out.println(result);

    }


}
